package com.tanhua.back.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.tanhua.back.service.AdminService;
import com.tanhua.back.utils.Constants;
import com.tanhua.back.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/11 15:00
 */
@RestController
@RequestMapping("/system/users")
public class SystemController {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private AdminService adminService;

    /**
     * 获取验证码图片
     * @param uuid
     * @param response
     * @throws IOException
     */
    @GetMapping("/verification")
    public void verification(String uuid, HttpServletResponse response) throws IOException {
        //1.生成验证码
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(299, 97);
        //2.验证码存入redis
        String code= captcha.getCode();
        redisTemplate.opsForValue().set(Constants.CAP_CODE+uuid,code);
        //3.输出验证码图片
        captcha.write(response.getOutputStream());
    }

    /**
     * 登陆
     * @param map
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map<String,String> map){
        Map<String,String> reMap=adminService.login(map);
        return ResponseEntity.ok(reMap);
    }

    /**
     * 登出
     */
    @PostMapping("/logout")
    public void logout(){
        adminService.logout();
    }

    /**
     * 查询用户基本信息
     * @return
     */
    @PostMapping("/profile")
    public AdminVo profile(){
        return adminService.profile();
    }
}
